#include "caculatedialog.h"

/**
 * @brief CaculateDialog::CaculateDialog
 * 构造函数
 */
CaculateDialog::CaculateDialog()
{
    // 标题
    this->setWindowTitle("计算器");
    this->setFixedHeight(60);

    // 左操作数
    m_editx = new QLineEdit(this);
    m_editx->setAlignment(Qt::AlignRight);
    m_editx->setValidator(new QDoubleValidator());// 验证器

    // 右操作数
    m_edity = new QLineEdit(this);
    m_edity->setAlignment(Qt::AlignRight);
    m_edity->setValidator(new QDoubleValidator());

    // 结果
    m_editz = new QLineEdit(this);
    m_editz->setAlignment(Qt::AlignRight);
    m_editz->setValidator(new QDoubleValidator());
    m_editz->setReadOnly(true);

    m_label = new QLabel("+",this);

    m_btn = new QPushButton("计算",this);
    m_btn->setEnabled(false);

    // 布局器
    QHBoxLayout *layout = new QHBoxLayout(this);
    layout->addWidget(m_editx);
    layout->addWidget(m_label);
    layout->addWidget(m_edity);
    layout->addWidget(m_btn);
    layout->addWidget(m_editz);

    setLayout(layout);

    // 连接信号和槽函数
    QObject::connect(m_editx,SIGNAL(textChanged(QString)),this,SLOT(enableButton(void)));
    QObject::connect(m_edity,SIGNAL(textChanged(QString)),this,SLOT(enableButton(void)));
    QObject::connect(m_btn,SIGNAL(clicked(void)),this,SLOT(calcClicked(void)));

}


/**
 * @brief CaculateDialog::enableButton
 * 按钮是否有效的槽函数
 */
void CaculateDialog::enableButton()
{
    bool b1 = false,b2 = false;
    m_editx->text().toDouble(&b1);
    m_edity->text().toDouble(&b2);
    m_btn->setEnabled(b1 && b2);
}

/**
 * @brief CaculateDialog::calcClicked
 * 具体的计算槽函数
 */
void CaculateDialog::calcClicked()
{
    double a = m_editx->text().toDouble();
    double b = m_edity->text().toDouble();
    double sum = a + b;
    QString str = QString::number(sum);

    m_editz->setText(str);

}
